package com.putong35.common.baseactfrag.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.putong35.common.R;
import com.putong35.common.baseactfrag.refreshable.IRefreshable;
import com.putong35.common.baseactfrag.refreshable.OnRefreshListener;
import com.putong35.common.baseactfrag.refreshable.swiperefresh.SwipeRefreshViewHelper;

import java.util.List;


public abstract class SwipeListFragment extends ListFragment implements IRefreshable {
    private SwipeRefreshViewHelper mSwipeRefreshViewHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mSwipeRefreshViewHelper = new SwipeRefreshViewHelper(getActivity(), this);
        View view = super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }


    @Override
    public View createRecyclerContentView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.base_common_swipelayout_recyclerview_content, null);
        RecyclerView rv = (RecyclerView) view.findViewById(R.id.recyclerview);
        setRecyclerView(rv);
        View refreshView = view.findViewById(R.id.refreshLayout);
        mSwipeRefreshViewHelper.initRefreshView(refreshView, this);
        return view;
    }


    /**
     * 回调方法，开始刷新
     */
    @Override
    public void onStartRefreshing() {
    }


    /**
     * 隐藏刷新控件
     */
    @Override
    public void hideRefreshView() {
        mSwipeRefreshViewHelper.hideRefreshView();
    }


    /**
     * 显示刷新控件
     */
    @Override
    public void showRefreshView() {
        mSwipeRefreshViewHelper.showRefreshView();
    }


    /**
     * 设置是否需要下拉刷新的功能
     *
     * @param enable
     */
    @Override
    public void setPullToRefreshEnable(boolean enable) {
        mSwipeRefreshViewHelper.setPullToRefreshEnable(enable);
    }

    /**
     * 设置向下滑动刷新的listener
     * user一般不需要设置这个东西，回调onStartRefreshing足够了。
     *
     * @param listener
     */
    @Override
    public void setOnRefreshListener(OnRefreshListener listener) {
        mSwipeRefreshViewHelper.setOnRefreshListener(listener);
    }

    @Override
    public void onListDataUpdateSuccess(List list) {
        super.onListDataUpdateSuccess(list);
        hideRefreshView();
    }

    @Override
    public void onListDataUpdateFailed(int code, String msg) {
        super.onListDataUpdateFailed(code, msg);
        hideRefreshView();
    }
}
